package com.kizitonwose.calendar.core

import androidx.compose.ui.text.intl.Locale
import androidx.compose.ui.text.toLowerCase
import kotlinx.datetime.DayOfWeek

internal fun firstDayFromMap(locale: Locale): DayOfWeek =
    firstDays[locale.region]?.let { firstDayValues[it] } ?: DayOfWeek.MONDAY

private val enLocale = Locale("en-US")
private var firstDayValues = DayOfWeek.entries.associateBy {
    it.name.toLowerCase(enLocale).take(3)
}

// https://github.com/unicode-org/cldr-json/blob/main/cldr-json/cldr-core/supplemental/weekData.json#L7
private val firstDays = mapOf(
    "001" to "mon",
    "AD" to "mon",
    "AE" to "sat",
    "AF" to "sat",
    "AG" to "sun",
    "AI" to "mon",
    "AL" to "mon",
    "AM" to "mon",
    "AN" to "mon",
    "AR" to "mon",
    "AS" to "sun",
    "AT" to "mon",
    "AU" to "mon",
    "AX" to "mon",
    "AZ" to "mon",
    "BA" to "mon",
    "BD" to "sun",
    "BE" to "mon",
    "BG" to "mon",
    "BH" to "sat",
    "BM" to "mon",
    "BN" to "mon",
    "BR" to "sun",
    "BS" to "sun",
    "BT" to "sun",
    "BW" to "sun",
    "BY" to "mon",
    "BZ" to "sun",
    "CA" to "sun",
    "CH" to "mon",
    "CL" to "mon",
    "CM" to "mon",
    "CN" to "mon",
    "CO" to "sun",
    "CR" to "mon",
    "CY" to "mon",
    "CZ" to "mon",
    "DE" to "mon",
    "DJ" to "sat",
    "DK" to "mon",
    "DM" to "sun",
    "DO" to "sun",
    "DZ" to "sat",
    "EC" to "mon",
    "EE" to "mon",
    "EG" to "sat",
    "ES" to "mon",
    "ET" to "sun",
    "FI" to "mon",
    "FJ" to "mon",
    "FO" to "mon",
    "FR" to "mon",
    "GB" to "mon",
    "GB-alt-variant" to "sun",
    "GE" to "mon",
    "GF" to "mon",
    "GP" to "mon",
    "GR" to "mon",
    "GT" to "sun",
    "GU" to "sun",
    "HK" to "sun",
    "HN" to "sun",
    "HR" to "mon",
    "HU" to "mon",
    "ID" to "sun",
    "IE" to "mon",
    "IL" to "sun",
    "IN" to "sun",
    "IQ" to "sat",
    "IR" to "sat",
    "IS" to "mon",
    "IT" to "mon",
    "JM" to "sun",
    "JO" to "sat",
    "JP" to "sun",
    "KE" to "sun",
    "KG" to "mon",
    "KH" to "sun",
    "KR" to "sun",
    "KW" to "sat",
    "KZ" to "mon",
    "LA" to "sun",
    "LB" to "mon",
    "LI" to "mon",
    "LK" to "mon",
    "LT" to "mon",
    "LU" to "mon",
    "LV" to "mon",
    "LY" to "sat",
    "MC" to "mon",
    "MD" to "mon",
    "ME" to "mon",
    "MH" to "sun",
    "MK" to "mon",
    "MM" to "sun",
    "MN" to "mon",
    "MO" to "sun",
    "MQ" to "mon",
    "MT" to "sun",
    "MV" to "fri",
    "MX" to "sun",
    "MY" to "mon",
    "MZ" to "sun",
    "NI" to "sun",
    "NL" to "mon",
    "NO" to "mon",
    "NP" to "sun",
    "NZ" to "mon",
    "OM" to "sat",
    "PA" to "sun",
    "PE" to "sun",
    "PH" to "sun",
    "PK" to "sun",
    "PL" to "mon",
    "PR" to "sun",
    "PT" to "sun",
    "PY" to "sun",
    "QA" to "sat",
    "RE" to "mon",
    "RO" to "mon",
    "RS" to "mon",
    "RU" to "mon",
    "SA" to "sun",
    "SD" to "sat",
    "SE" to "mon",
    "SG" to "sun",
    "SI" to "mon",
    "SK" to "mon",
    "SM" to "mon",
    "SV" to "sun",
    "SY" to "sat",
    "TH" to "sun",
    "TJ" to "mon",
    "TM" to "mon",
    "TR" to "mon",
    "TT" to "sun",
    "TW" to "sun",
    "UA" to "mon",
    "UM" to "sun",
    "US" to "sun",
    "UY" to "mon",
    "UZ" to "mon",
    "VA" to "mon",
    "VE" to "sun",
    "VI" to "sun",
    "VN" to "mon",
    "WS" to "sun",
    "XK" to "mon",
    "YE" to "sun",
    "ZA" to "sun",
    "ZW" to "sun",
)
